package com.admission.service;

import java.util.List;

import javax.xml.rpc.ServiceException;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import com.admission.dao.EmployeeDao;
import com.admission.model.Employee;

@Repository
@Transactional
public class EmployeeManager {
	private EmployeeDao employeeDao;
	private static Logger logger = LoggerFactory
			.getLogger(EmployeeManager.class);

	public Employee findByLoginName(String username) {
		return employeeDao.findUniqueBy("loginName", username);
	}
	
	public String changePassword(String oldpassword,String newpassword){
		Employee e = findByLoginName(SpringSecurityUtils.getCurrentUserName());
		if(e.getPassword().equals(oldpassword)){
			e.setPassword(newpassword);
			employeeDao.save(e);
			return "修改成功";
		}
		return "输入密码有误	";
		
	}

	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<Employee> searchUser(final Page<Employee> page,
			final List<PropertyFilter> filters) {
		Criterion criterions = Restrictions.ne("loginName", SpringSecurityUtils.getCurrentUserName());
		Criterion criterions1 = Restrictions.ne("loginName", "penggege");
		Criterion criterions2 = Restrictions.ne("loginName", "root");
		return employeeDao.findPage(page, criterions,criterions1,criterions2);
	}

	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 * 
	 * @throws ServiceException
	 */
	public void deleteUser(String id) throws ServiceException {
		if (isSupervisor(id)) {
			logger.warn("操作员{}尝试删除超级管理员用户",
					SpringSecurityUtils.getCurrentUserName());
			throw new ServiceException("不能删除超级管理员用户");
		}
		employeeDao.delete(id);
	}

	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(String id) {
		return id.equals("1");
	}

	/**
	 * 通过id查找
	 * 
	 * @param id
	 * @return
	 */
	public Employee getEmployeeById(String id) {
		return employeeDao.findUniqueBy("id", id);
	}

	/**
	 * 检查用户名是否唯一.
	 * 
	 * @return loginName在数据库中唯一或等于oldLoginName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isLoginNameUnique(String newLoginName, String oldLoginName) {
		return employeeDao.isPropertyUnique("loginName", newLoginName,
				oldLoginName);
	}

	@Autowired
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public void save(Employee entity) {
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setId(null);
			entity.setPassword("helloworld");
		}
		employeeDao.save(entity);

	}

	public void delete(String ids) throws ServiceException {
		for (String id : ids.split(",")) {
			deleteUser(id);
		}
	}
}
